﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Web.UI.WebControls;
using weikebuluo.Model;
using weikebuluo.DAL;

namespace weikeweixin.web.admin.wechat
{
    public partial class defualt : weikeweixin.web.admin.cs.adminbase
    {
        w_article_dal articledal = new w_article_dal();
        protected DataTable dtarticle = new DataTable();

        w_reply_dal replydal = new w_reply_dal();
        protected w_reply_model replymodel = new w_reply_model();

        protected void Page_Load(object sender, EventArgs e)
        {
            dtarticle = articledal.GetList("useer_id=" + user_id + " and article_layid=0").Tables[0];
            DataTable dtreply;
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                dtreply = replydal.GetList("wechat_id=" + wechat_id + " and reply_fangshi="+Request.QueryString["id"]).Tables[0];
                if (dtreply.Rows.Count > 0)
                {
                    replymodel = replydal.GetModel(int.Parse(dtreply.Rows[0]["reply_id"].ToString()));
                }
            }

            switch (Request.Form["action"]) { 
                case"add":
                    add();
                    break;
            }
        }

        private void add() {
            DataTable dtreply = replydal.GetList("wechat_id=" + wechat_id + " and reply_fangshi=" + Request.Form["id"]).Tables[0];
            if (dtreply.Rows.Count > 0)
            {
                replymodel = replydal.GetModel(int.Parse(dtreply.Rows[0]["reply_id"].ToString()));
            }
            replymodel.reply_type = Request.Form["reply_type"];
            switch (Request.Form["reply_type"]) {
                case "text":
                    replymodel.reply_text = Request.Form["keyword_text"];
                    break;
                case "news":
                    replymodel.article_id = int.Parse(Request.Form["article_id"].Trim().ToString());
                    break;
            }
            replymodel.wechat_id = int.Parse(wechat_id);
            replymodel.reply_fangshi = int.Parse(Request.Form["id"].Trim().ToString());

            if (dtreply.Rows.Count > 0)
            {
                bool re = replydal.Update(replymodel);
                if (re)
                {
                    Response.Write("{\"errno\":\"0\",\"tip\":\"修改成功！\",\"url\":\"defualt.aspx?id=" + Request.Form["id"] + "\",\"error\":\"\"}");
                    Response.End();
                }
                else
                {
                    Response.Write("{\"errno\":\"0\",\"tip\":\"保存失败！\",\"url\":\"defualt.aspx?id=" + Request.Form["id"] + "\",\"error\":\"\"}");
                    Response.End();
                }
            }
            else {
                int re = replydal.Add(replymodel);
                if (re > 0)
                {
                    Response.Write("{\"errno\":\"0\",\"tip\":\"保存成功！\",\"url\":\"defualt.aspx?id=" + Request.Form["id"] + "\",\"error\":\"\"}");
                    Response.End();
                }
                else
                {
                    Response.Write("{\"errno\":\"0\",\"tip\":\"保存失败！\",\"url\":\"defualt.aspx?id=" + Request.Form["id"] + "\",\"error\":\"\"}");
                    Response.End();
                }
            }
        }
    }

}